﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;


namespace WPFDemo.Converters
{
    /// <summary>
    ///  BooleanToVisibilityコンバーター. コントロールを表示、未表示する為のプロパティ
    /// </summary>
    public class BooleanToVisibility : IValueConverter
    {
        /// <summary>
        /// 値’によってVisibilityステータスが返す
        /// </summary>
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is bool))
            {
                value = System.Convert.ToBoolean(value);
            }
            return ConvertValue((bool)value);
        }

        /// <summary>
        /// ConverCollapsed機能を呼び出す、Visibilityが返す
        /// </summary>
        /// <param name="value"></param>
        protected virtual Visibility ConvertValue(bool value)
        {
            return ConvertCollapsed(value);
        }

        /// <summary>
        /// VisibilityをBooleanに交換する
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// isVisibleに応じてVisibilityを返す為の内部メソッド
        /// </summary>
        /// <param name="isVisible"></param>
        /// <returns></returns>
        internal static Visibility ConvertCollapsed(bool isVisible)
        {
            return isVisible ?
                        Visibility.Visible :
                        Visibility.Collapsed;
        }
    }

}
